package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: Miyako
 * @Date: 2024-07-30-15:40
 * @Description 使用jdbc进行查询操作并将数据通过ResultSet类的next()与get方法输出到控制台
 */
public class JDBCDemo05 {
    public static void main(String[] args) {
        try (Connection connection = new DBUtil("tedu").getConnection()) {
            Statement statement = connection.createStatement();
            String sql = "SELECT username,password,nickname,age FROM userinfo";
            ResultSet resultSet = statement.executeQuery(sql);
            System.out.println("username" + "\t " + "password" + "\t " + "nickname" + "\t " + "age");
            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String password = resultSet.getString("password");
                String nickname = resultSet.getString("nickname");
                int age = resultSet.getInt("age");
                System.out.println(username + "\t " + password + "\t " + nickname + "\t " + age);
            }
            resultSet.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
